package bmatch.tech.todo.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: 格式工具类
 * @author:LinJinhao
 * @create:2018-12-28 10:26
 **/
public class FormatUtils {

    /**
     * 验证邮箱格式
    * @Param: email
    * @return:
    **/
    public static boolean checkEmail(String email){
        if (email == null){
            return false;
        }
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern pat;
        Matcher mat;
        pat = Pattern.compile(check);
        mat = pat.matcher(email);

        if (mat.matches()){
            return true;
        }else {
            return false;
        }

    }

    /**
     * 验证密码格式
     * 包含字母、数字以及其他符号
     * 6-18位
    * @Param: pwd
    * @return:
    **/
    public static boolean checkPwd(String pwd) {
        if (pwd == null) return false;
        if (pwd.length() < 6 || pwd.length() > 18) return false;
        /*合法的组成部分*/
        //String[]collectPart= {"_","@","&","#","$","."};
        String part = "@&#$,.";
        for (int i = 0; i < pwd.length(); i++) {
            /*如果不为字母和数字*/
            if (!(Character.isLetterOrDigit(pwd.charAt(i)))) {
                if (!(part.contains(Character.toString(pwd.charAt(i))))) {
                    return false;
                }
            }
        }
        return true;
    }
}
